import { getMenuList, addMenu, updateMenu, updateStatusMenu, getRoleForMenuRight, getSystemMenu } from '@/services/settings/menu/systemMenuService';

export default {
    namespace: 'systemMenuModel',

    state: {},

    effects: {
        *fetch({ payload, callback }, { call, put }) {
            const response = yield call(getMenuList, payload);
            if (callback) callback(response);
        },
        *add({ payload, callback }, { call, put }) {
            const response = yield call(addMenu, payload);
            if (callback) callback(response);
        },
        *update({ payload, callback }, { call, put }) {
            const response = yield call(updateMenu, payload);
            if (callback) callback(response);
        },
        *updateStatus({ payload, callback }, { call, put }) {
            const response = yield call(updateStatusMenu, payload);
            yield put({
                type: 'RCallback',
                response: response,
            });
            if (callback) callback(response);
        },
        *getRoleMenu({ payload, callback }, { call, put }) {
          const response = yield call(getRoleForMenuRight, payload);
          if(callback) callback(response);
        },
        *getSysMenu({payload, callback}, {call, put}) {
          const token = sessionStorage.getItem("MYRCIB_USER_TOKEN");
          if(token) {
            const response = yield call(getSystemMenu);
            if (callback) callback(response);
          }else{
            window.location.href = "/user/login";
          }
        },
    },

    reducers: {
        RCallback(state, action) {
            return {
                ...state,
                data: action.response.datas,
                requestFinish: true,
            };
        },
    },
};
